import DB from '../../util/db';
import dbCfg from '../../../etc/db';
import * as util from '../../util';

const db = new DB(dbCfg);
export default db;


/**
 * 数据库模型配置
 * */
db.set('sys_appsettings', {id: 'id'});
db.set('sys_logs', {id: 'id'});
db.set('sys_dicts', {id: 'id'});
db.set('sys_users', {
    id: 'id',
    ques: function a() {
        return this.hasMany(db.get('xp_questionnaires'), ['userId']);
    },
    institution: function a() {
        return this.belongsTo(db.get('sys_institutions'), 'institutionId');
    },
    serialize: function() {// eslint-disable-line
        const result = this.constructor.__super__.serialize.apply(this, arguments);// eslint-disable-line
        if (result && result.birthday) result.birthday = util.formatDateTime(result.birthday, true);
        return result;
    },
});
db.set('sys_feedbacks', {
    id: 'id',
    user: function a() {
        return this.belongsTo(db.get('sys_users'), 'userId');
    }
});
db.set('xp_newstypes', {id: 'id'});
db.set('xp_news', {id: 'id'});
db.set('xp_products', {
    id: 'id',
    images: function a() {
        return this.hasMany(db.get('xp_productimages'), ['productId']);
    },
});
db.set('xp_productimages', {id: 'id'});
db.set('xp_categories', {id: 'id'});

db.set('xp_questionnairetemplates', {
    id: 'id',
    questions: function a() {
        return this.hasMany(db.get('xp_questions'), ['queTempId']);
    },
    ques: function a() {
        return this.hasMany(db.get('xp_questionnaires'), ['queTempId']);
    }
});
db.set('xp_questionoptions', {
    id: 'id',
    product: function a() {
        return this.belongsTo(db.get('xp_products'), 'productId');
    }
});
db.set('xp_questions', {
    id: 'id',
    options: function a() {
        return this.hasMany(db.get('xp_questionoptions'), ['questionId']);
    }
});
db.set('xp_answers', {id: 'id'});
db.set('xp_questionnaires', {
    id: 'id',
    user: function a() {
        return this.belongsTo(db.get('sys_users'), 'userId');
    },
    queTemp: function a() {
        return this.belongsTo(db.get('xp_questionnairetemplates'), 'queTempId');
    },
    questions: function a() {
        return this.hasMany(db.get('xp_questions'), ['queTempId']);
    },
    answers: function a() {
        return this.hasMany(db.get('xp_answers'), ['queId']);
    }
});

/************************活动模块****************************/
db.set('sys_institutions', {
    id: 'id',
    adminUser: function a() {
        return this.belongsTo(db.get('sys_users'), 'adminUserId');
    },
    pInstitution: function a() {
        return this.belongsTo(db.get('sys_institutions'), 'pid');
    },
});
db.set('xp_activity', {
    id: 'id',
    images: function a() {
        return this.hasMany(db.get('xp_activityimages'), ['activityId']);
    },
    institution: function a() {
        return this.belongsTo(db.get('sys_institutions'), 'institutionId');
    },
});
db.set('xp_activityimages', {id: 'id'});
db.set('xp_policy', {
    id: 'id',
    salesman: function a() {
        return this.belongsTo(db.get('sys_users'), 'salesmanId');
    },
    activity: function a() {
        return this.belongsTo(db.get('xp_activity'), 'activityId');
    },
    institution: function a() {
        return this.belongsTo(db.get('sys_institutions'), 'institutionId');
    },
});
db.set('xp_activitytype', {id: 'id'});
db.set('xp_customers', {
    id: 'id',
    salesman: function a() {
        return this.belongsTo(db.get('sys_users'), 'salesmanId');
    },
    activity: function a() {
        return this.belongsTo(db.get('xp_activity'), 'activityId');
    },
    institution: function a() {
        return this.belongsTo(db.get('sys_institutions'), 'salesman.institutionId');
    },
});
db.set('xp_activity_votes', {
    id: 'id',
    option: function a() {
        return this.belongsTo(db.get('sys_users'), 'optionId');
    },
    customer: function a() {
        return this.belongsTo(db.get('xp_customers'), 'customerId');
    }
});
